package com.phone580.app.register;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.phone580.app.BaseActivity;
import com.phone580.app.BaseTask.TaskCallback;
import com.phone580.app.R;
import com.phone580.app.entity.RegisterEntity;
import com.phone580.app.untilty.StringUtil;

public class RegisterActivity extends BaseActivity implements TaskCallback{
	private EditText etUser;
	private EditText etEmail;
	private EditText etPwd;
	private EditText etRePwd;
	private Button btnCommit;
	private TextView tvLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_layout);
		initHeader("会员注册");
		etUser = (EditText) findViewById(R.id.user_et);
		etPwd = (EditText) findViewById(R.id.pwd_et);
		etRePwd = (EditText) findViewById(R.id.re_pwd_et);
		etEmail = (EditText) findViewById(R.id.email_et);
		btnCommit = (Button) findViewById(R.id.reg_commit_btn);
		tvLogin = (TextView) findViewById(R.id.register_login_tv);
		tvLogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		btnCommit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (checkForm()) {
					RegisterTask task = new RegisterTask(RegisterActivity.this);
					task.setParams(etUser.getText().toString(), etPwd.getText().toString(), etEmail.getText().toString());
					task.setTaskCallback(RegisterActivity.this);
					task.execute();
				}
			}
		});
	}
	
	/**
	 * 验证注册表单
	 * @return
	 */
	public boolean checkForm() {
		String username = etUser.getText().toString();
		if (StringUtil.isNullOrEmpty(username)) {
//			etUser.setError("用户名不能为空");
			Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (username.length() < 6) {
//			etUser.setError("用户名长度至少6个字符");
			Toast.makeText(this, "用户名长度至少6个字符", Toast.LENGTH_SHORT).show();
			return false;
		}
		String email = etEmail.getText().toString();
		if (StringUtil.isNullOrEmpty(email)) {
//			etEmail.setError("邮件地址不能为空");
			Toast.makeText(this, "邮件地址不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (!email.contains("@") || !email.contains(".")) {
//			etEmail.setError("邮件地址格式有错");
			Toast.makeText(this, "邮件地址格式有错", Toast.LENGTH_SHORT).show();
			return false;
		}
		String pwd = etPwd.getText().toString();
		if (StringUtil.isNullOrEmpty(pwd)) {
//			etPwd.setError("密码不能为空");
			Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (pwd.length() < 6) {
//			etPwd.setError("密码长度至少6个字符");
			Toast.makeText(this, "密码长度至少6个字符", Toast.LENGTH_SHORT).show();
			return false;
		}
		String rePwd = etRePwd.getText().toString();
		if (StringUtil.isNullOrEmpty(rePwd)) {
//			etRePwd.setError("请再输一次密码");
			Toast.makeText(this, "请再输一次密码", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (!rePwd.equals(pwd)) {
//			etRePwd.setError("两次输入的密码不一致");
			Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	@Override
	public void execute(Object result) {
		if (result != null) {
			RegisterEntity entity = (RegisterEntity)result;
			Toast.makeText(this, entity.getMessage(), Toast.LENGTH_LONG).show();
			if (entity.isSuccess()) {
				finish();
			}
		} else {
			showNetworkError();
		}
	}
}
